package net.xxcxcxcx.xc_enhancedmob.mixin.client;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.text.Text;
import net.minecraft.util.math.Matrix4f;
import net.xxcxcxcx.xc_enhancedmob.component.BaseComponent;
import net.xxcxcxcx.xc_enhancedmob.component.EnhancedBaseComponent;
import net.xxcxcxcx.xc_enhancedmob.data.MobData;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

@Mixin(EntityRenderer.class)
public abstract class EntityRendererMixin<T extends Entity> {
    @Shadow
    @Final
    protected EntityRenderDispatcher dispatcher;

    @Shadow
    public abstract TextRenderer getFontRenderer();

    @Shadow
    protected abstract boolean hasLabel(T entity);

    //显示更多行的UI
    @Inject(method = "render", at = @At(value = "HEAD"))
    public void morerender(T entity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo ci) {
        if (!this.hasLabel(entity)) {
            return;
        }
        if (entity instanceof MobEntity) {
            BaseComponent basecomponent = ((MobData) entity).getComponent(EnhancedBaseComponent.class.getName());
            if (basecomponent instanceof EnhancedBaseComponent component && component.hasMoreText()) {
                List<Text> list = component.getMoretext();
                for (int i = 0; i < list.size(); i++) {
                    renderPresent(entity, list.get(i), matrices, vertexConsumers, light, list.size() - i);
                }
            }
        }
    }

    protected void renderPresent(T entity, Text text, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int move) {
        double d = this.dispatcher.getSquaredDistanceToCamera(entity);
        if (d > 4096.0) {
            return;
        }
        boolean bl = !entity.isSneaky();
        float f = entity.getHeight() + 0.5f;
        int i = ("deadmau5".equals(text.getString()) ? -10 : 0) + (-10) * move;
        matrices.push();
        matrices.translate(0.0, f, 0.0);
        matrices.multiply(this.dispatcher.getRotation());
        matrices.scale(-0.025f, -0.025f, 0.025f);
        Matrix4f matrix4f = matrices.peek().getModel();
        float g = MinecraftClient.getInstance().options.getTextBackgroundOpacity(0.25f);
        int j = (int) (g * 255.0f) << 24;
        TextRenderer textRenderer = this.getFontRenderer();
        float h = -textRenderer.getWidth(text) / 2;
        textRenderer.draw(text, h, (float) i, 0x20FFFFFF, false, matrix4f, vertexConsumers, bl, j, light);
        if (bl) {
            textRenderer.draw(text, h, (float) i, -1, false, matrix4f, vertexConsumers, false, 0, light);
        }
        matrices.pop();
    }
}
